//Controls initial item and NPC drop as well as NPC movement

	GM = GM or gmod.GetGamemode()
	GM.doodad = {}
	GM.doodad.items = {
		crystal = .4,
		meat = .1,
		rock = 1,
		vine = .5,
		wood = .8,
	}
	GM.doodad.npcs = {
		npc_crow = 1,
		npc_pigeon = 1,
		npc_seagull = .01,
	}
	
GM.randomFromTable = function(tab,results)
	local total = 0
	local out = {}
	
	for k,v in pairs(tab) do
		total = total + v
	end
	
	for i = 1,math.ceil(results) do
		local a = math.random(0,total*10000)/10000
		local b = 0
		for k,v in pairs(tab) do
			b = b + v
			if a < b then
				table.insert(out,k)
				break
			end
		end
	end
	return out
end

GM.randomVector = function(range)
	local out = Vector(0,0,0)
	for k,v in pairs{"x","y","z"} do
		if range[v] > 0 then
			out[v] = math.random(0,range[v])
		else
			out[v] = math.random(range[v],0)
		end
	end
	return out
end

GM.doodadSpawnItems = function()
	local self = gmod.GetGamemode()
	
	//Find the spawn areas
	areas = {}
	for i,area in pairs(ents.FindByClass("info_target")) do
		if area:GetName():sub(0,14) == "survival_spawn" then
			local parent = area:GetParent()
			if IsValid(parent) then
				areas[area] = parent
			end
		end
	end
	
	for pAe,pBe in pairs(areas) do
		//Calculate volume
		local pA,pB = pAe:GetPos(),pBe:GetPos()
		
		local vol = math.abs((pA.x-pB.x)*(pA.y-pB.y)*(pA.z-pB.z))
		local range = pA - pB
		
		local range_items = 1/524288 * ( gmod.GetGamemode().doodad.multiplier or 1 )
		local range_npcs = 1/2097152
		
		//Spawn items
		for k,v in pairs(self.randomFromTable(self.doodad.items,range_items*vol)) do
			local item = self.items[v]
			local drop = ents.Create("ws_item")
			drop:SetModel(item.model)
			drop:GetTable().class = v
			local pos = self.randomVector(range) + pB
			drop:SetPos(pos)
			drop:Spawn()
		end
		
		//Spawn npcs
		for k,v in pairs(self.randomFromTable(self.doodad.npcs,range_npcs*vol)) do
			local drop = ents.Create(v)
			local pos = self.randomVector(range) + pB
			drop:SetPos(pos)
			drop:Spawn()
			self.bird[drop] = {
				lastflight = (k*60)/(range_npcs*vol) + CurTime() - 60,
				lastscare = CurTime()-20, //To prevent scare from drop
			}
			
			//Birds fly every 60 seconds
		end
	end
end

--"Artificial Stupidiness
GM.bird = {}
hook.Add("Think","wsBirdAI",function()
	local self = gmod.GetGamemode()
	
	local toremove = {}
	for k,v in pairs(self.bird) do
		if k:IsValid() then
			if CurTime()-30 > v.lastscare then
				//Birds like to fly away if something around them moves, right?
				
				/* Key speeds:
					500	Run jump
					320	Run
					200	Normal
					120	Duck normal
					75	Walk
					15	Duck walk
					
					SCALAR
					
					6 feet by ducking
					120 inches per second = 72 inches * 1.67
					6 feet by running
					320 = 191.62 inches * 1.67
					
					EXPONENTIAL
					120
					14400 iips = 7*12 * 170.67
					
					320
					102400 iips = 50*12 * 170.67
					
					This $exponential$ calculation wins the money!
					I did my homework on this one, Termy. They get scared by projectiles easily.
					Although, they should be scared by weapon swings, too...
				*/
				
				for i,o in pairs(ents.FindInSphere(k:GetPos(),600)) do //50 feet
					if IsValid(o) and o ~= k and o:GetClass() ~= "ws_pigeon" then
						//Birds tend to flock together, so don't filter them out unless they are landing. Plus: Group scares!
						if not (self.doodad.npcs[o:GetClass()] and o:GetVelocity().z < 10) then
							local speed = o:GetVelocity():Length()
							local dist = (o:GetPos() - k:GetPos()):Length()
							if speed^2 > dist * 170.67 then
								//OH SNAP SOMEONE IS GOING TO KILL ME
								self:birdFlyAway(k)
								
								//You have 60 seconds to find the worm. It may be underwater. Good luck, birdy! (******)
								v.lastflight = CurTime()
								v.lastscare = CurTime()
								
								break
							end
						end
					end
				end
				
				v.lastscare = CurTime()-30 + math.random(1,30)/100 //So that we don't use FindInSphere too often
			end
			
			if CurTime()-60 > v.lastflight then
				self:birdFlyAway(k)
				
				//You have 60 seconds to find the worm. It may be underwater. Good luck, birdy! (******)
				v.lastflight = CurTime()
			end
		else
			table.insert(toremove,k)
		end
	end
	for k,v in pairs(toremove) do
		table.remove(self.bird,k)
	end
end)

--"Fly away!
function GM:birdFlyAway(bird)
	//Let's fly for no g*d d*m*ed reason! (Stupid birds!)
	
	//Find a random area to carry a coconut to while still being able to maintain airspeed velocity!
	//(I haven't worked out the part about calculating if they can maintain airspeed velocity)
	local sel = math.random(1,table.Count(areas))
	local i = 0
	local pA,pB
	for pAe,pBe in pairs(areas) do
		i = i+1
		//Assume the bird is an african swallow and can maintain airspeed velocity
		if i == sel then
			pA = pAe:GetPos()
			pB = pBe:GetPos()
			break
		end
	end
	
	if pA and pB then
		//Msg("Flying to area ",sel,"!\n") //If you have more than one area and birds only go to one, check this.
		
		//Calculate the exact, random spot that we plan to find worms (which may happen to be underwater) (***king birds)
		local wormpos = self.randomVector(pA-pB)+pB
		
		//Create the rallypoint where the bird will prepare its assault
		wsUID = wsUID + 1
		local rally = ents.Create("assault_rallypoint")
		rally:SetName("wsrally_" .. tostring(wsUID))
		rally:SetPos(wormpos)
		timer.Simple(2,function()
			if IsValid(rally) then
				rally:Remove()
			end
		end)
		
		//ATTACK!!!
		bird:Fire("FlyAway",rally:GetName(),0)
	end
end

--"Fresh meat!
hook.Add("OnNPCKilled","wsBirdKilled",function(bird,killer,weapon)
	local self = gmod.GetGamemode()
	if IsValid(bird) and self.doodad.npcs[bird:GetClass()] then
		for i = 1,math.random(1,3) do
			local item = self.items.meat
			local drop = ents.Create("ws_item")
			drop:SetModel(item.model)
			drop:GetTable().class = "meat"
			drop:SetPos(bird:GetPos()+Vector(0,0,8))
			drop:Spawn()
		end
	end
end)

/*
--"Bring a paring knife!
hook.Add("PlayerUse","wsBirdUsed",function(pl,bird) //Isn't called on NPCs
	//if bird:GetClass():Find("npc") then
		//Msg("USED ",bird:GetClass(),"\n")
	//end
	
	if not (IsValid(bird) and bird:IsNPC() and IsValid(pl) and pl:IsPlayer()) then
		return
	end
	local self = gmod.GetGamemode()
	
	if bird:GetNPCState() == NPC_STATE_DEAD then //OH MY GOD IT IS DEAD, SOMEONE CALL A PETA REP
		//(And by call, I mean ki**).
		
		wsUID = wsUID + 1
		self.buildingdata[wsUID] = {
			wsUID = wsUID,
			ents = {
				bird,
			},
			class = bird:GetClass(),
		}
		local tab = self.buildingdata[wsUID]
		bird:GetTable().wsUID = wsUID
		
		self:BuildingGive(wsUID,"meat",math.random(1,3))
	end
end)
*/








